#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/add_two_ints.hpp"

class ServiceServer_01 : public rclcpp::Node
{
public:
    ServiceServer_01(std::string name) : Node(name)
    {
        RCLCPP_INFO(this->get_logger(), "I am service server 01: %s", name.c_str());
        server_ = this->create_service<example_interfaces::srv::AddTwoInts>("add_tow_ints_srv", std::bind(&ServiceServer_01::_addTwoIntsCallback, this, std::placeholders::_1, std::placeholders::_2));
    }

private:
    void _addTwoIntsCallback(std::shared_ptr<example_interfaces::srv::AddTwoInts::Request> request, std::shared_ptr<example_interfaces::srv::AddTwoInts::Response> response)
    {
        RCLCPP_INFO(this->get_logger(), "Get two int:a=%ld, b=%ld", request->a, request->b);
        response->sum = request->a + request->b;
        rclcpp::sleep_for(std::chrono::seconds(5));
        std::cout << "spin thread pid===" << std::this_thread::get_id() << std::endl;
    }

private:
    rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr server_{};
};

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<ServiceServer_01>("service_server_01");
    rclcpp::spin(node);
    rclcpp::shutdown();
    std::cout << "main thread pid===" << std::this_thread::get_id() << std::endl;

    return 0;
}
